<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加相册</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
<!--model	表单数据对象  rules	表单验证规则-->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <!--  rules对象的属性名称与prop的名称要对应匹配    -->
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AddAlbumView",
  data() {
    return {
      ruleForm: {
        name: '测试',
        description: '暂无',
        sort: '0',
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序序号', trigger: 'blur' },
          { pattern: /^(\d{1}|[1-9]{1}[0-9]?})$/,message: '排序序号必须是0~99之间的数字'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      /*validate：对整个表单进行校验的方法，参数为一个回调函数。
      该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段。
      若不传入回调函数，则会返回一个 promise*/
      //遍历每一个值
      this.$refs[formName].validate((valid) => {
        if (valid) {

          let url = 'http://localhost:9080/albums/add-new'
          console.log('url:' + url)
          let formData = this.qs.stringify(this.ruleForm);
          console.log('请求参数')
          //不要写字符串的拼接，因为无法展开参数内容
          console.log(formData)

          this.axios
              .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then(response=>{
            let responseBody = response.data
            console.log(responseBody)
            if(responseBody.state == 20000){
              this.$message.success("添加成功")
              //进行调试的时候不要加重置表单，数据打桩输出会影响
              this.resetForm(formName)
            }else {
              this.$message.error(responseBody.message)
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>